package com.wh.wisdomsite.xbox.utils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class Tools {
	/**
	 * 检测字符串是否不为空(null,"","null")
	 * @param s
	 * @return 不为空则返回true，否则返回false
	 */
	public static boolean notEmpty(String s){
		return s!=null && !"".equals(s) && !"null".equals(s);
	}
	
	/**
	 * 检测字符串是否为空(null,"","null")
	 * @param s
	 * @return 为空则返回true，不否则返回false
	 */
	public static boolean isEmpty(String s){
		return s==null || "".equals(s) || "null".equals(s);
	}
	
	/**
	 * 字符串转换为字符串数组
	 * @param str 字符串
	 * @param splitRegex 分隔符
	 * @return
	 */
	public static String[] str2StrArray(String str,String splitRegex){
		if(isEmpty(str)){
			return null;
		}
		return str.split(splitRegex);
	}
	
	/**
	 * 用默认的分隔符(,)将字符串转换为字符串数组
	 * @param str	字符串
	 * @return
	 */
	public static String[] str2StrArray(String str){
		return str2StrArray(str,",\\s*");
	}
	
	/**
	 * 按照yyyy-MM-dd HH:mm:ss的格式，日期转字符串
	 * @param date
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static String date2Str(Date date){
		return date2Str(date, "yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	 * 按照yyyy-MM-dd HH:mm:ss的格式，字符串转日期
	 * @param date
	 * @return
	 */
	public static Date str2Date(String date){
        String format = "yyyy-MM-dd HH:mm:ss";
		return str2Date(date,format);
	}
    /**
     * 按照#{format}的格式，字符串转日期
     * @param date
     * @return
     */
    public static Date str2Date(String date,String format){
        if(notEmpty(date)){
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            try {
                return sdf.parse(date);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return new Date();
        }else{
            return null;
        }
    }
	
	/**
	 * 按照参数format的格式，日期转字符串
	 * @param date
	 * @param format
	 * @return
	 */
	public static String date2Str(Date date,String format){
		if(date!=null){
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.format(date);
		}else{
			return "";
		}
	}
	//MD5密码加密工具
	public static String getMD5Str(String str){
	      MessageDigest messageDigest = null;
	      try {
			messageDigest = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	      messageDigest.reset();
	      try {
			messageDigest.update(str.getBytes("gbk"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    byte[] byteArray = messageDigest.digest();
	    StringBuffer md5StrBuff = new StringBuffer();
	    for (int i = 0; i < byteArray.length; i++) {
	      if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
	        md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
	      else
	        md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
	    }
	    return md5StrBuff.toString();
	  }
    /**
     * 将字符串转化成日期
     */
    public static Date strDate(String date){
        if(notEmpty(date)){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                return sdf.parse(date);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return new Date();
        }else{
            return null;
        }
    }
    /*10位补0*/
    public static String addToTenString(String code){
          if(notEmpty(code)){
              String code_str=new String();
              int vb=code.length();
              if(vb<10){
                  int num=(10-vb);
                  for(int i=0;i<num;i++){
                      code_str=("0"+code_str).toString();
                  }
                  code=(code_str+code).toString();
              }
          }
          return code;
    }

    private static long[] ls = new long[3000];
    private static int li = 0;

    public synchronized static long getPK() {
        long lo = getpk();
        for (int i = 0; i < 3000; i++) {
            long lt = ls[i];
            if (lt == lo) {
                lo = getPK();
                break;
            }
        }
        ls[li] = lo;
        li++;
        if (li == 3000) {
            li = 0;
        }
        return lo;
    }

    private static long getpk() {
        String a = (String.valueOf(System.currentTimeMillis())).substring(3, 10);
        // String d = (String.valueOf(Math.random())).substring(2, 5);
        Random rand = new Random();
        String d = Integer.toString((rand.nextInt(9) + 1) * 100 + rand.nextInt(99));  //保证生成流水号为10位
        return Long.parseLong(d + a);
    }

    /**
     * 获取随即吗
     * @param length
     * @return
     */
    public static String getRandomCode(int length) {
        if(length<=0) length=1;
        String randomNoteStrs = "0123456789";
        char[] chars = randomNoteStrs.toCharArray();
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<length;i++){
            Double d = Math.random()*10;
            sb.append(chars[d.intValue()]);
        }
        return sb.toString();
    }



    /**
     * 金额处理
     * @param b
     * @return
     */
    public static BigDecimal isBigDecimal(BigDecimal b){
        if(null == b){
            return new BigDecimal(0);
        }
        return  b;
    }
}
